package com.fourinone;

import com.fourinone.bean.ResourceBean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * @Description
 * @auther Szper
 * @create 2019-10-18 14:02
 */
public class SettingBean extends ResourceBean {
    private String nativeLangCode;
    public SettingBean(String langCode) {
        super();
        resourcesName = "config";
        init(langCode);
    }
    public void init(String langCode)
    {
        if(langCode==null)
        {
            bundle = ResourceBundle.getBundle(resourcesName, Locale.getDefault());
        }
        else if(langCode.toUpperCase().equals("ISO-8859-1"))
        {
            nativeLangCode = "ISO-8859-1";
            bundle = ResourceBundle.getBundle(resourcesName, Locale.US);
        }
        else if(langCode.toUpperCase().equals("GB2312"))
        {
            nativeLangCode = "GB2312";
            bundle = ResourceBundle.getBundle(resourcesName, Locale.PRC);
        }
        else if(langCode.toUpperCase().equals("BIG5"))
        {
            nativeLangCode = "BIG5";
            bundle = ResourceBundle.getBundle(resourcesName, Locale.TAIWAN);//new Locale("zh", "TW");
        }
    }

    public String getString(String keyWord)
    {
        return getString(keyWord, "");
    }

    public String getString(String keyWord, String topStr)
    {
        String str = "";
        try
        {
            str = bundle.getString(keyWord);

        }
        catch(MissingResourceException ex)
        {
            str = topStr+keyWord;
            //System.err.println(ex);
        }
        return str;
    }
    public String getSpace()
    {
        String space = "";
        if(nativeLangCode!=null&&nativeLangCode.equals("ISO-8859-1"))
            space = "&nbsp;";

        return space;
    }

    public String getFileString(String relativeUri){
        StringBuffer sb = new StringBuffer();
        try{
            Reader f = new InputStreamReader(this.getClass().getResourceAsStream(relativeUri));
            BufferedReader fb = new BufferedReader(f);
            String s = "";
            while((s=fb.readLine())!=null){
                sb = sb.append(s);
            }
            f.close();
            fb.close();
        }catch(IOException ex){
            //System.err.println(ex);
        }
        //System.out.println(sb);
        return sb.toString();
    }
    public static void main(String[] args) {
        SettingBean sb = new SettingBean("ISO-8859-1");
    }

}
